# -*- coding: utf-8 -*-
# @Project : APITestModel
# @File    : HttpCommonRequest.py
# @Author  : 朱宽
# @Time    : 2021/4/6 9:59
# @Software: Win10 / Python3 / Pycharm
import requests
from config import globalConfig


class HttpRequests():
    '''
    http请求的公共调用方式。后续遇到更多请求方式时，请依据软件工程中“高内聚，低耦合”的原则进行扩展。
    '''

    def __init__(self):
        self.protocol = globalConfig.http_protocol
        self.host = globalConfig.http_host
        self.port = globalConfig.http_port
        if self.port != None:
            self.url_header = self.protocol + "://" + self.host + ":" + self.port
        else:
            self.url_header = self.protocol + "://" + self.host
        self.path = globalConfig.project_directory_path + '/HttpCommonRequest'

    def send_request(self, data):
        '''
        封装发送RestFul风格的http请求。get、post、put、delete、patch
        :param data:
        :return:
        '''
        try:
            re = None

            url = self.url_header + data['Uri']
            method = data['Method'].lower()
            header = {'Content-Type': data['Content-Type']}  # http数据包头部构建
            send_data = data['SendData']  # 发送数据

            if method == 'get':  # get请求
                if send_data != None:
                    re = requests.get(url, params=send_data, headers=header)  # get带参数请求
                else:
                    re = requests.get(url, headers=header)  # get不带参数请求

            elif method == 'post':  # post请求
                if header['Content-Type'] == 'application/x-www-form-urlencoded':
                    if send_data != None:
                        re = requests.post(url, data=send_data, headers=header)
                    else:
                        re = requests.post(url, headers=header)

                elif header['Content-Type'] == 'text/xml':
                    if send_data != None:
                        re = requests.post(url, json=send_data, headers=header)
                    else:
                        re = requests.post(url, headers=header)

                elif header['Content-Type'] == 'application/json':
                    if send_data != None:
                        re = requests.post(url, json=send_data, headers=header)
                    else:
                        re = requests.post(url, headers=header)

                elif header['Content-Type'] == 'multipart/form-data':
                    if send_data != None:
                        re = requests.post(url, data=send_data, headers=header)
                    else:
                        re = requests.post(url, headers=header)

                else:
                    print('post方式不存在这种传递模式，请在下面位置扩展：', self.path, '--56行')

            elif method == 'put':  # put请求
                if header['Content-Type'] == 'application/x-www-form-urlencoded':
                    if send_data != None:
                        re = requests.put(url, data=send_data, headers=header)
                    else:
                        re = requests.put(url, headers=header)

                elif header['Content-Type'] == 'text/xml':
                    if send_data != None:
                        re = requests.put(url, json=send_data, headers=header)
                    else:
                        re = requests.put(url, headers=header)

                elif header['Content-Type'] == 'application/json':
                    if send_data != None:
                        re = requests.put(url, json=send_data, headers=header)
                    else:
                        re = requests.put(url, headers=header)

                elif header['Content-Type'] == 'multipart/form-data':
                    if send_data != None:
                        re = requests.put(url, data=send_data, headers=header)
                    else:
                        re = requests.put(url, headers=header)

                else:
                    print('put方式不存在这种传递模式，请在下面位置扩展：', self.path, '--68行')

            elif method == 'delete':  # delete请求
                if header['Content-Type'] == 'application/x-www-form-urlencoded':
                    if send_data != None:
                        re = requests.delete(url, data=send_data, headers=header)
                    else:
                        re = requests.delete(url, headers=header)

                elif header['Content-Type'] == 'text/xml':
                    if send_data != None:
                        re = requests.delete(url, json=send_data, headers=header)
                    else:
                        re = requests.delete(url, headers=header)

                elif header['Content-Type'] == 'application/json':
                    if send_data != None:
                        re = requests.delete(url, json=send_data, headers=header)
                    else:
                        re = requests.delete(url, headers=header)

                elif header['Content-Type'] == 'multipart/form-data':
                    if send_data != None:
                        re = requests.delete(url, data=send_data, headers=header)
                    else:
                        re = requests.delete(url, headers=header)

                else:
                    print('delete方式不存在这种传递模式，请在下面位置扩展：', self.path, '--80行')

            elif method == 'patch':  # patch请求
                re = requests.patch(url, headers=header)

            else:
                print('不存在这种方法，请在下面位置扩展：', self.path, '--74行')
            return re
        except Exception as e:
            print(self.path, '  ', e)


if __name__ == "__main__":
    data = {
            "TestCaseFlag": "S341Command",
            "CorrectFlag": True,
            "Method": "put",
            "Uri": "/api/config/instruction",
            "Content-Type": 'application/x-www-form-urlencoded',
            "SendData": {
                'appId':'TQ',
                'cmdCode':'ht-test',
                'cmdId':'0001',
                'imei':'000111122223333',
                'offLineInsType':'normalIns',
                'platform':'TQ',
                'proNo':'80',
                'requestId':'0002',
                'sendTime':'1623741522000',
                'sender':'test-ht',
                'taskId':'0001',
                'timeOut':'10',
                'userId':'343222'
            },
            "AssertInfo": {
                "code": 100000,
                "msg": "成功"
            },
            "CaseDescription": "查询deviceAppID",
            "Rely": "yes",
            "Extract": None,
            "MockFlag": None,
            "MockData": None
        }
    test = HttpRequests()
    re = test.send_request(data)
    print(re.json())

